import geolocation from '@system.geolocation'

export default function() {
  return new Promise(resolve => {
    geolocation.getLocation({
      timeout: 1000,
      success(response) {
        try {
          geolocation.reverseGeocodeQuery({
            longitude: response.longitude,
            latitude: response.latitude,
            success(result) {
              resolve({
                ...response,
                ...result
              })
            },
            fail(e) {
              console.error(e)
              resolve({})
            }
          })
        } catch (e) {
          resolve(response)
        }
      },
      fail(e) {
        console.error(e)
        resolve({})
      }
    })
  })
}
